/**角色动作动画
 * author:noodles
 * */
package com.gamebase.role
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import com.gamebase.role.vo.Direction;

	public class AvatarMC extends Bitmap
	{
		private var _partBMDVector:Vector.<Vector.<BitmapData>>;//帧系列
		private var frameTimer:Timer;
		private var frameRec:Rectangle;
		private var backPlayAble:Boolean = false;//是否可以回播
		private var _direction:String;
		private var _totalFrames:uint;//图片列数
		private var _currentFrame:uint;
		private var endDelayTimer:Timer;//回放延时时钟
		private var _onEnterFrame:Function;
		
		/**
		 * @param partList:角色的身体部位图片列表，堆垒时，靠前的部位先堆垒
		 * @param rec:单帧图片大小矩形
		 * @param $onEnterFrame:每播放一帧时触发的函数
		 * @param direction:初始站立方向
		 * */
		public function AvatarMC(fullBmd:BitmapData, rec:Rectangle, $onEnterFrame:Function=null, direction:String=null)
		{
			super(new BitmapData(rec.width,rec.height,true));
			frameRec = rec;
			frameRec.x = 0;
			frameRec.y = 0;
			_onEnterFrame = $onEnterFrame;
			_totalFrames = Math.ceil(fullBmd.width/frameRec.width);
			//持久化各帧的位图数据,保存在_partBMDVector中
			_partBMDVector = AvatarUtils.serializationBMD(fullBmd,rec);
			
			if(direction != null)
			{
				this.gotoAndStop(1,direction);
			}
			frameTimer = new Timer(250/*1000/24*/);
			frameTimer.addEventListener(TimerEvent.TIMER,onFrameTimerEvent);
		}
		
		private function standDirection(direction:String):void
		{
			if(_direction == direction)
			{
				return;
			}
			
			
			var lastBMD:BitmapData = this.bitmapData;
			var lineIndex:uint = 0;//第几行
			_direction = direction;
			switch(_direction)
			{
				case Direction.Front:
				{
					this.bitmapData = _partBMDVector[2][_currentFrame];
					this.reverse(false);
					break;
				}
				case Direction.Back:
				{
					this.bitmapData = _partBMDVector[3][_currentFrame];
					this.reverse(false);
					break;
				}
				case Direction.Left:
				{
					this.bitmapData = _partBMDVector[4][_currentFrame];
					this.reverse(false);
					break;
				}
				case Direction.Right:
				{
					this.bitmapData = _partBMDVector[4][_currentFrame];
					this.reverse(true);
					break;
				}
				case Direction.LeftBack:
				{
					this.bitmapData = _partBMDVector[1][_currentFrame];
					this.reverse(true);
					break;
				}
				case Direction.RightBack:
				{
					this.bitmapData = _partBMDVector[1][_currentFrame];
					this.reverse(false);
					break;
				}
				case Direction.RightFront:
				{
					this.bitmapData = _partBMDVector[0][_currentFrame];
					this.reverse(true);
					break;
				}
				case Direction.LeftFront:
				{
					this.bitmapData = _partBMDVector[0][_currentFrame];
					this.reverse(false);
					break;
				}
				
			}
			
			//matrix.ty = -frameRec.height*lineIndex;
			//this.drawRec();
			if(lastBMD)
			{
				lastBMD.dispose();
			}
		}
		
		/**
		 * @param frame : 最小值为1
		 * */
		public function gotoAndStop(frame:uint,direction:String=null):void
		{
			
			if(frame<1) 
				frame = 1;
			else if(frame>_totalFrames)
				frame = _totalFrames;
			_currentFrame = frame;
			if(direction != null)
			{
				this.standDirection(direction);
			}
			//this.drawRec();
			
		}
		
		/**
		 * @param frame : 最小值为1
		 * */
		public function gotoAndPlay(frame:uint,direction:String=null):void
		{
			gotoAndStop(frame,direction);
			
			//如果动画不止一帧
			if((_totalFrames > 1) && (!frameTimer.running))
			{
				frameTimer.start();
			}
		}
		
		
		
		public function play(direction:String=null):void
		{
			if(direction != null)
			{
				this.standDirection(direction);
			}
			//如果动画不止一帧
			if((_totalFrames > 1) && (!frameTimer.running))
			{
				frameTimer.start();
			}
		}
		
		/**循环播放一次
		 * @param endDelay:播放到最后一帧时的延时(单位：秒)
		 * */
		public function playCircularly(endDelay:Number=0.033):void
		{
			endDelay = endDelay * 1000;
			if(endDelayTimer == null)
			{
				endDelayTimer = new Timer(endDelay,1);
				endDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE,startBackPlay);
			}
			else
			{
				endDelayTimer.delay = endDelay;
				endDelayTimer.reset();
			}
			backPlayAble = true;
			gotoAndPlay(1);
		}
		
		/**循环播放一次
		 * @param remainTime:动画播放维持时间，如果大于正常来回播放一次的时间，则剩余的时间停留在最后一帧(单位：秒)
		 * */
		public function playTween(remainTime:Number=0.033):void
		{
			var endDelay:Number = (remainTime * 1000) - _totalFrames * 2 * frameTimer.delay;
			if(endDelay < 0) endDelay = 0;
			playCircularly(endDelay/1000);
		}
		
		private function startBackPlay(evt:TimerEvent):void
		{
			gotoAndPlay(_totalFrames);
		}
		
		public function stop(direction:String=null):void
		{
			if(direction != null)
			{
				this.standDirection(direction);
			}
			if(frameTimer.running)
			{
				frameTimer.stop();
			}
		}
		
		public function get totalFrames():uint
		{
			return _totalFrames;
		}
		
		public function get currentFrame():uint
		{
			return _currentFrame;
		}
		
		/**帧切换
		 * 必须为多帧动画才允许触发
		 * */
		private function onFrameTimerEvent(evt:TimerEvent):void
		{
			if(backPlayAble)
			{
				gotoAndStop(_currentFrame - 1);
				//如果已经播放到最后一帧
				if(_currentFrame == _totalFrames)
				{
					stop();
					if(endDelayTimer.delay >0)
					{
						endDelayTimer.reset();
						endDelayTimer.start();
					}
						
				}
					
				//如果倒播到第一帧
				if(_currentFrame == 1)
				{
					backPlayAble = false;
					stop();
				}
			}
			else
			{
				gotoAndStop(_currentFrame + 1);
				//如果已经播放到最后一帧
				if(_currentFrame == _totalFrames)
					this.gotoAndPlay(1);
			}
			if(_onEnterFrame != null)
				_onEnterFrame();
		}
		
		//翻转处理
		private function reverse(r:Boolean=true):void
		{
			if(r)
			{
				this.scaleX = -1;
				this.x = frameRec.width;
			}
			else
			{
				this.scaleX = 1;
				this.x = 0;
			}
		}
		
		public function destroy():void
		{
			
			if(frameTimer != null)
			{
				frameTimer.removeEventListener(TimerEvent.TIMER,onFrameTimerEvent);
				frameTimer.stop();
				frameTimer = null;
			}
			if(endDelayTimer != null)
			{
				endDelayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,startBackPlay);
				endDelayTimer.stop();
				endDelayTimer = null;
			}
			_partBMDVector = null;
			this.bitmapData.dispose();
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
	}
}